package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hqyj.dao.AttendanceSheetMapper;
import com.hqyj.pojo.AttendanceSheet;
import com.hqyj.pojo.SalaryScale;
import com.hqyj.dao.SalaryScaleMapper;
import com.hqyj.service.ISalaryScaleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.xml.bind.attachment.AttachmentMarshaller;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
 * @since 2021-10-20
 */
@Service
public class SalaryScaleServiceImpl extends ServiceImpl<SalaryScaleMapper, SalaryScale> implements ISalaryScaleService {
    @Resource
    SalaryScaleMapper salaryScaleMapper;

    @Resource
    AttendanceSheetMapper attendanceSheetMapper;

    @Override
    public HashMap<String, Object> querySararyList(SalaryScale salaryScale) {

        System.out.println("salary========="+salaryScale);

        HashMap<String,Object> map = new HashMap<>();
        int pagefirst = (salaryScale.getPage()-1) * salaryScale.getRow();
        int pageSize = salaryScale.getRow();
        List<SalaryScale> list = null;
        if (salaryScale.getSalaryId() != null && salaryScale.getSalaryId() != 0){
            list = salaryScaleMapper.queryByOneId(salaryScale.getSalaryId());
        }else {
            list = salaryScaleMapper.querySalaryList(pagefirst, pageSize);
        }


        int total = salaryScaleMapper.querytotal();
        double d = salaryScale.getRow();
        int totalPage = (int)Math.ceil(total/d);

        map.put("list",list);

        map.put("total",total);
        map.put("totalPage",totalPage);
        map.put("curPage",salaryScale.getPage());

        //下一页
        if (salaryScale.getPage() == totalPage){
            map.put("nextPage",totalPage);
        }else {
            map.put("nextPage",salaryScale.getPage()+1);
        }

        //上一页
        if (salaryScale.getPage() == 1){
            map.put("prePage",1);
        }else {
            map.put("prePage",salaryScale.getPage()-1);
        }

        return map;

    }

    @Override
    public HashMap<String, Object> insertSarary(SalaryScale salaryScale) {
        HashMap<String,Object> map = new HashMap<>();
        int insert = salaryScaleMapper.insertSarary(salaryScale);
        if (insert == 1){
            map.put("info","保存成功");
        }else {
            map.put("info","保存失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> moneySarary(SalaryScale salaryScale) {

        Set<String> set = new HashSet<>();
        set.add("1");
        set.add("2");

        for (String s : set) {
            
        }
        
        
        HashMap<String,Object> map = new HashMap<>();
        Integer id = salaryScale.getSalaryId();

        int totalmoney = attendanceSheetMapper.totalmoney(id);
        System.out.println(totalmoney);

        List<Integer> list = attendanceSheetMapper.selectawardMoney(id);
        System.out.println(list);
        int award = 0;
        for (Integer integer : list) {
            award = award + integer;
        }

        //算出这个人的基本工资＋绩效工资
        List<SalaryScale> salaryScales = salaryScaleMapper.queryByOneId(salaryScale.getSalaryId());
        SalaryScale one = salaryScales.get(0);
        int sumMoney =Integer.valueOf(one.getBasePay())+Integer.valueOf(one.getMeritPay())+totalmoney;

        salaryScale.setBonus(String.valueOf(award));
        salaryScale.setNumberSalary(String.valueOf(sumMoney));
        int fine = award - totalmoney;
        salaryScale.setFine(String.valueOf(fine));

        int i = salaryScaleMapper.updateOneSarary(salaryScale);

        if (i == 1){
            map.put("info","结算成功");
        }else {
            map.put("info","结算失败");
        }

        return map;
    }
    public int A(){
        return 0;
    }
    public void A(int i){}

   
}
